package com.example.project.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.example.project.base.BaseServiceImpl;
import com.example.project.entity.TAdmin;
import com.example.project.entity.TAdmin_Role;
import com.example.project.mapper.TAdminMapper;
import com.example.project.mapper.TAdminRoleMapper;
import com.example.project.service.TAdminService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import tk.mybatis.mapper.entity.Example;

import java.util.Map;

@Service
public class TAdminServiceImpl extends BaseServiceImpl<TAdmin> implements TAdminService {
    @Autowired
    TAdminMapper tAdminMapper;
    @Autowired
    TAdminRoleMapper tAdminRoleMapper;

    private static Logger logger = LoggerFactory.getLogger(TAdminServiceImpl.class);

    public PageInfo<TAdmin> getAllAdminPageInfo(Integer pageNo, Integer pageSize){

        PageHelper.startPage(pageNo==null?0:pageNo,pageSize==null?0:pageSize);
//        Map<String,Boolean> map=new HashMap(){{
//            put("sort",true);
//        }};
//        return new PageInfo<TAdmin>(super.queryByWhereAndOrderBy(TAdmin.class,map,null));

        return new PageInfo<>(tAdminMapper.queryAllInforBySort());
    }

    public void updateAdminSort(Integer pkid,Integer sort,String type) throws Exception {
        if(type.equals("asc")){
            TAdmin tAdminLast=tAdminMapper.getAdminBySort(pkid,sort,"desc");
            if(tAdminLast!=null){
                //升序操作
                TAdmin tAdmin=new TAdmin();
                tAdmin.setSort(tAdminLast.getSort());
                tAdmin.setPkid(pkid);

                tAdminLast.setSort(sort);
//                tAdminMapper.updateByPrimaryKey(tAdmin);
//                tAdminMapper.updateByPrimaryKey(tAdminLast);
                if(tAdminLast.getSort().equals(tAdmin.getSort()) ){
                    tAdminLast.setSort(tAdminLast.getSort()-1);
                }

                super.updateSortById(tAdmin.getPkid(),tAdmin.getSort());
                super.updateSortById(tAdminLast.getPkid(),tAdminLast.getSort());

            }else{
                logger.warn("提升序列失败");
            }
        }else if(type.equals("desc")){
            //降序操作
            TAdmin tAdminNext=tAdminMapper.getAdminBySort(pkid,sort,"asc");
            if(tAdminNext!=null){
                TAdmin tAdmin=new TAdmin();
                tAdmin.setSort(tAdminNext.getSort());
                tAdmin.setPkid(pkid);

                tAdminNext.setSort(sort);
//                logger.debug("{}",JSONObject.toJSONString(tAdmin));
//                tAdminMapper.updateByPrimaryKey(tAdmin);
                if(tAdminNext.getSort().equals(tAdmin.getSort())){
                    tAdminNext.setSort(tAdminNext.getSort()+1);
                }

                super.updateSortById(tAdmin.getPkid(),tAdmin.getSort());

//                logger.debug("{}",JSONObject.toJSONString(tAdminNext));
                super.updateSortById(tAdminNext.getPkid(),tAdminNext.getSort());


//                tAdminMapper.updateByPrimaryKey(tAdminNext);
            }else{
                logger.warn("降低序列失败");
            }
        }else{
            throw new Exception("非法参数");
        }
    }
    @Transactional
    public Boolean addNewAdmin(TAdmin tAdmin,Integer roleId){
        try {
            int sort=super.selectMaxSort();
            tAdmin.setSort(++sort);
            tAdminMapper.insert(tAdmin);
            TAdmin_Role tAdminRole=new TAdmin_Role(){{setAdminId(tAdmin.getPkid());setRoleId(roleId);}};

            tAdminRoleMapper.insert(tAdminRole);


            return true;
        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();

            return false;
        }
    }

    public Map<String,Object> getUserInfoById(Integer pkid){

        return tAdminMapper.getUserInfoById(pkid);
    }

    public void updateNewAdmin(TAdmin tAdmin,Integer roleId) throws Exception{

//        tAdminMapper.updateByPrimaryKey(tAdmin);
        tAdminMapper.updateAdmin(tAdmin);

        Example example = new Example(TAdmin_Role.class);
        example.createCriteria().andEqualTo("adminId", tAdmin.getPkid());

        tAdminRoleMapper.updateByExample(new TAdmin_Role(){{setRoleId(roleId);setAdminId(tAdmin.getPkid());}},example);

    }
}
